﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZX.Web.UI.Controls;

namespace ZX.WebApp.EHR
{
    public partial class hrRecruitmentAjax : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string flag = this.GetStringFromQueryString("flag", "");
            switch (flag.Trim())
            {
                case "RecruitmentDetailSave"://保存招聘计划明细信息
                    string json = this.GetStringFromQueryString("json", string.Empty);
                    string id = this.GetStringFromQueryString("id", string.Empty);
                    string RecruitmentPlanID=this.GetStringFromQueryString("RecruitmentPlanID", string.Empty);
                    string fun = this.GetStringFromQueryString("fun", string.Empty);
                    if(RecruitmentPlanID==string.Empty || fun==string.Empty)
                    {
                        HttpContext.Current.Response.Write("-1");
                        return;
                    }
                    if (json==string.Empty && id == string.Empty)
                    {
                        HttpContext.Current.Response.Write("-1");
                        return;
                    }
                    List<string> list = new List<string>();
                    if (id != string.Empty)
                    {
                        list.Add(id);
                    }
                    else
                    {
                        string[] strarr = json.Split(',');
                        foreach (var s in strarr)
                        {
                            list.Add(s);
                        }
                    }
                    try
                    {
                        ZX.EHR.Library.hrRequirement instance = ZX.Common.Globals.getEntity("hrRequirement") as ZX.EHR.Library.hrRequirement;
                        ZX.BLL.Extend.BaseBussnies hrBll = new ZX.BLL.Extend.BaseBussnies(instance);
                        foreach (string s in list)
                        {
                            string condition = "ID='" + s + "'";
                            hrBll = new ZX.BLL.Extend.BaseBussnies(instance);
                            ZX.EHR.Library.hrRequirement obj = hrBll.GetEntityByCondition(condition) as ZX.EHR.Library.hrRequirement;
                            if (fun == "add")
                            {
                                obj.RecruitmentPlanID = new Guid(RecruitmentPlanID);
                            }
                            else if(fun=="del")
                            {
                                obj.RecruitmentPlanID = Guid.Empty;
                            }
                            hrBll.Update(obj);
                            //更新招聘计划人数
                            ZX.EHR.Library.hrRecruitmentPlan instance2 = ZX.Common.Globals.getEntity("hrRecruitmentPlan") as ZX.EHR.Library.hrRecruitmentPlan;
                            hrBll = new ZX.BLL.Extend.BaseBussnies(instance2);
                            condition = "ID='" + RecruitmentPlanID + "'";
                            ZX.EHR.Library.hrRecruitmentPlan obj2 = hrBll.GetEntityByCondition(condition) as ZX.EHR.Library.hrRecruitmentPlan;
                            if (fun == "add")
                            {
                                obj2.RecruitmentNum += obj.AvailableNumber;
                            }
                            else if (fun == "del")
                            {
                                obj2.RecruitmentNum -= obj.AvailableNumber;
                            }                          
                            hrBll.Update(obj2);
                        }
                        HttpContext.Current.Response.Write("0");
                    }
                    catch (Exception ex)
                    {
                        HttpContext.Current.Response.Write("e_" + ex.Message);
                    }
                    break;
            }
        }
    }
}